package com.zbf.wenizYanZhengMa;

import com.zbf.wenizYanZhengMa.zhongwen.ChineseCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author: LCG
 * @date: 2023-03-28 21:11:53
 * @description: 汉字算术验证码
 **/
@RestController
@RequestMapping("suan")
public class HanZiSuanShuController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping("getCode")
    public void getCode(HttpServletResponse response) throws IOException {
        ChineseCaptcha chinese = new ChineseCaptcha();
        BufferedImage image1 = chinese.create("");
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        // 将随机汉字存在session中
        // stringRedisTemplate.opsForValue().set("picode",random);
        // 将图形写给浏览器
        response.setContentType("image/jpeg");
        // 发头控制浏览器不要缓存
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        // 将图片写给浏览器
        ImageIO.write(image1, "jpg", response.getOutputStream());
    }

}
